import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from python_ai.common.xcommon import sep

plt.rcParams['font.sans-serif'] = ['Simhei']
plt.rcParams['axes.unicode_minus'] = False
pd.set_option('display.max_columns', None, 'display.expand_frame_repr', False)

sep('data')
df = pd.read_csv(r'../../../../../large_data/ML2/air_data.csv')
print(df.shape)

sep('select not null and not zero')
data = df[(df['SUM_YR_1'].notna())&(df['SUM_YR_2'].notna())]
print(data.shape)
index1 = data['SUM_YR_1'] > 0
index2 = data['SUM_YR_2'] > 0
index3 = (data['SEG_KM_SUM'] != 0) & (data['avg_discount'] != 0)
data = data[index1 & index2 & index3]
print(data.shape)

sep('select columns')
data = data[
    ["FFP_DATE", "LOAD_TIME",
     "FLIGHT_COUNT",
     "SUM_YR_1", "SUM_YR_2",
     "SEG_KM_SUM",
     "AVG_INTERVAL", "MAX_INTERVAL",
     "avg_discount"]
]
# "FFP_DATE" 入会注册时间,
# "LOAD_TIME",上次登陆时间
#  "FLIGHT_COUNT",飞行次数
# "SUM_YR_1",第一年消费总额
# "SUM_YR_2",第二年消费总额
# "SEG_KM_SUM",飞行总里程
# "AVG_INTERVAL" , 平均乘坐飞机的时间间隔（对该乘客而言，正常乘坐飞机的间隔）
# "MAX_INTERVAL", 最大乘坐飞机的时间间隔
# "avg_discount" 平均折扣率

sep('入会时间长度(days)')
print(data[:5][["FFP_DATE", "LOAD_TIME"]])
data['FFP_DATE'] = pd.to_datetime(data['FFP_DATE'])
data['LOAD_TIME'] = pd.to_datetime(data['LOAD_TIME'])
print(data[:5][["FFP_DATE", "LOAD_TIME"]])
data['入会时间长度'] = data['LOAD_TIME'] - data['FFP_DATE']
print(data[:5][["FFP_DATE", "LOAD_TIME", '入会时间长度']])
data['入会时间长度'] = data['入会时间长度'].astype(np.int64) // (3600 * 24 * 1e9)
print(data[:5][["FFP_DATE", "LOAD_TIME", '入会时间长度']])

sep('平均每公里票价')
data['平均每公里票价'] = (data['SUM_YR_1'] + data['SUM_YR_2']) / data['SEG_KM_SUM']
print(data[:5]['平均每公里票价'])

sep('时间间隔差值')
print(data[:5][['AVG_INTERVAL', 'MAX_INTERVAL']])
data['时间间隔差值'] = data['MAX_INTERVAL'] - data['AVG_INTERVAL']
print(data[:5][['AVG_INTERVAL', 'MAX_INTERVAL', '时间间隔差值']])

sep('columns')
data = data.rename(columns={'FLIGHT_COUNT': '飞行次数',
                            'SEG_KM_SUM': '总里程',
                            'avg_discount': '平均折扣率'})
data = data[
    ["入会时间长度",
     "飞行次数",
     "平均每公里票价",
     "总里程",
     "时间间隔差值",
     "平均折扣率"]
]
columns = data.columns
n_columns = len(columns)
idx = data.index
data = data[idx % 20 == 0]  # For testing only, comment out this in formal code

sep('Scale')
from sklearn.preprocessing import StandardScaler
std = StandardScaler()
x = std.fit_transform(data)

sep('Visualization')
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
plt.figure(figsize=[12, 8])
spr = 2
spc = 3
spn = 0
ks = range(4, 6+1)
sse_list = []
sil_list = []
labels_list = []
angles = np.linspace(0, 2 * np.pi, n_columns + 1, endpoint=True)
angle_titles = columns
angle_titles = angle_titles.append(angle_titles[0:1])
for k in ks:
    model = KMeans(n_clusters=k)
    labels = model.fit_predict(x)
    spn += 1
    plt.subplot(spr, spc, spn, projection='polar')
    plt.title(f'K = {k}')
    plt.xticks(angles, angle_titles)
    cs = model.cluster_centers_
    sse_list.append(model.inertia_)
    sil_list.append(silhouette_score(x, labels))
    labels_list.append(labels)
    cmap = plt.cm.get_cmap('rainbow', k)
    for i, c in enumerate(cs):
        c = np.r_[c, c[0]]
        plt.polar(angles, c, label=f'Class#{i}', color=cmap(i))
    plt.legend()

# sse
spn += 1
plt.subplot(spr, spc, spn)
plt.title('SSE')
plt.plot(ks, sse_list)

# silhouette
spn += 1
plt.subplot(spr, spc, spn)
plt.title('Silhouette')
plt.plot(ks, sil_list)

# pie of clustering k=5
idx = 1
spn += 1
ax = plt.subplot(spr, spc, spn)
plt.axis('off')
labels_s = pd.Series(labels_list[idx]).value_counts()
labels_s.index = [f'Class#{i}' for i in range(ks[idx])]
cmap = plt.cm.get_cmap('rainbow', ks[idx])
labels_s.plot(kind='pie', autopct='%.2f%%', ax=ax, cmap=cmap)

# Finally show all drawings.
plt.show()
